/**
 * 
 */
package ads.engenharia.bl;

import java.sql.SQLException;
import java.util.ArrayList;

import ads.engenharia.dal.DisciplineDAO;
import ads.engenharia.dal.DisciplineProfessorDAO;
import ads.engenharia.dal.DisciplineScheduleDAO;
import ads.engenharia.entity.Discipline;
import ads.engenharia.entity.Professor;
import ads.engenharia.entity.Schedule;

/**
 * @author Lanedraex
 *
 */
public class DisciplineBO {

	private DisciplineDAO disciplineDAO;
	private DisciplineScheduleDAO disciplineScheduleDAO;
	private DisciplineProfessorDAO disciplineProfessorDAO;

	/**
	 * 
	 * @param discipline
	 * @throws SQLException
	 */
	public void save(Discipline discipline) throws SQLException {

		disciplineDAO = new DisciplineDAO();
		disciplineScheduleDAO = new DisciplineScheduleDAO();
		disciplineProfessorDAO = new DisciplineProfessorDAO();

		discipline.id(disciplineDAO.save(discipline));
		disciplineScheduleDAO.save(discipline);
		// disciplineProfessorDAO.save(discipline);
	}

	/**
	 * 
	 * @param discipline
	 * @throws SQLException
	 */
	public void delete(Discipline discipline) throws SQLException {

		disciplineDAO = new DisciplineDAO();
		disciplineDAO.delete(discipline);

	}

	/**
	 * 
	 * @param discipline
	 * @throws SQLException
	 */
	public void update(Discipline discipline) throws SQLException {

		disciplineDAO = new DisciplineDAO();
		disciplineDAO.update(discipline);
		disciplineScheduleDAO.update(discipline);
		disciplineProfessorDAO.update(discipline);

	}

	/**
	 * 
	 * @param discipline
	 * @return
	 * @throws SQLException
	 */
	public Discipline search(Discipline discipline) throws SQLException {

		// Procura pelo schedule para completar a disciplina
		discipline.schedule(searchDisciplineSchedule(discipline));

		// Procura pelo professor para completar a disciplina
		discipline.professor(searchDisciplineProfessor(discipline));

		disciplineDAO = new DisciplineDAO();
		discipline = disciplineDAO.search(discipline);
		return discipline;
	}

	/**
	 * 
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<Discipline> searchAll() throws SQLException {

		disciplineDAO = new DisciplineDAO();
		return disciplineDAO.searchAll();

	}

	/**
	 * 
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public Discipline searchById(int id) throws SQLException {

		disciplineDAO = new DisciplineDAO();
		return disciplineDAO.searchById(id);

	}

	/**
	 * 
	 * @return
	 * @throws SQLException
	 */
	public Discipline searchByMaxId() throws SQLException {

		Discipline discipline;
		disciplineDAO = new DisciplineDAO();
		discipline = disciplineDAO.searchByMaxId();

		// Procura pelo schedule para completar a disciplina
		discipline.schedule(searchDisciplineSchedule(discipline));

		// Procura pelo professor para completar a disciplina
		discipline.professor(searchDisciplineProfessor(discipline));

		return discipline;

	}

	/**
	 * 
	 * @param name
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<Discipline> searchByName(String name) throws SQLException {

		ArrayList<Discipline> disciplines;
		disciplineDAO = new DisciplineDAO();
		disciplines = disciplineDAO.searchByName(name);

		for (Discipline d : disciplines) {
			// Procura pelo schedule para completar a disciplina
			d.schedule(searchDisciplineSchedule(d));

			// Procura pelo professor para completar a disciplina
			d.professor(searchDisciplineProfessor(d));

		}

		return disciplines;

	}

	/**
	 * 
	 * @param duration
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<Discipline> searchByDuration(int duration)
			throws SQLException {

		ArrayList<Discipline> disciplines;
		disciplineDAO = new DisciplineDAO();
		disciplines = disciplineDAO.searchByDuration(duration);

		for (Discipline d : disciplines) {
			// Procura pelo schedule para completar a disciplina
			d.schedule(searchDisciplineSchedule(d));

			// Procura pelo professor para completar a disciplina
			d.professor(searchDisciplineProfessor(d));

		}

		return disciplines;

	}

	// public ArrayList<Discipline> searchByDay(Schedule.Day day) {
	//
	// ArrayList<Discipline> disciplines;
	// disciplineDAO = new DisciplineDAO();
	// disciplines = disciplineDAO.searchByDuration(duration);
	//
	// for (Discipline d : disciplines) {
	// // Procura pelo schedule para completar a disciplina
	// d.schedule(searchDisciplineSchedule(d));
	//
	// // Procura pelo professor para completar a disciplina
	// d.professor(searchDisciplineProfessor(d));
	//
	// }
	//
	// return disciplines;
	// }

	/**
	 * 
	 * @param discipline
	 * @return
	 * @throws SQLException
	 */
	public Schedule searchDisciplineSchedule(Discipline discipline)
			throws SQLException {

		disciplineScheduleDAO = new DisciplineScheduleDAO();
		return disciplineScheduleDAO.searchDisciplineSchedule(discipline);
	}

	/**
	 * 
	 * @param discipline
	 * @return
	 * @throws SQLException
	 */
	public Professor searchDisciplineProfessor(Discipline discipline)
			throws SQLException {

		disciplineProfessorDAO = new DisciplineProfessorDAO();
		return disciplineProfessorDAO.searchDisciplineProfessor(discipline);
	}
}
